﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spaanjaars.ContactManager.Model;
using Spaanjaars.ContactManager.Services.Commands;
using Spaanjaars.ContactManager.Services.ViewModels;

namespace Spaanjaars.ContactManager.Services.Mappers
{
    public static class ContactPersonsViewModelMapper
    {
        public static IEnumerable<ContactPersonViewModel> ConvertAllToContactPersonViewModel(this IEnumerable<ContactPerson> contactPeople, bool mapAddresses)
        {
            List<ContactPersonViewModel> contactPersonViewModels = new List<ContactPersonViewModel>();

            foreach (ContactPerson person in contactPeople)
            {
                contactPersonViewModels.Add(person.ConvertToContactPersonViewModel(mapAddresses));
            }

            return contactPersonViewModels;
        }

        public static ContactPersonViewModel ConvertToContactPersonViewModel(this ContactPerson contactPerson, bool mapAddress)
        {
            var convertToContactPersonViewModel = new ContactPersonViewModel() {Name = contactPerson.Name.ToString(),
                                                                                DateOfBirth = contactPerson.DateOfBirth.ToString(), 
                                                                                Id = contactPerson.Id,};

            if (mapAddress)
            {                
                convertToContactPersonViewModel.Addresss = contactPerson.GetAddresses().ConvertAllToAddressViewModel(); 
            }

            return convertToContactPersonViewModel;
        }
        
        public static ContactPerson ConvertToContactPerson(this CreateContactPersonCommand contactPersonViewModel)
        {
            return new ContactPerson()
                       {                           
                           DateOfBirth = contactPersonViewModel.DateOfBirth,
                           Name = new Name(contactPersonViewModel.FirstName, contactPersonViewModel.MiddleName, contactPersonViewModel.LastName)
                       };
        }

        public static IEnumerable<AddressViewModel> ConvertAllToAddressViewModel(this IEnumerable<Address> addresses)
        {
            IList<AddressViewModel> addressViewModels = new List<AddressViewModel>();

            foreach (Address address in addresses)
                addressViewModels.Add(address.ConvertToAddresViewModel());

            return addressViewModels;
        }

        public static AddressViewModel ConvertToAddresViewModel(this Address address)
        {
            return new AddressViewModel()
                       {
                           HouseNumber = address.HouseNumber,
                           City = address.City,
                           Country = address.Country,
                           Street = address.Street,
                           ZipCode = address.ZipCode
                       };
        }
    }
}
